/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

/**
 *
 * @author Lone_Wolf
 */
public class EcranRecherche extends JPanel {

    private Fenetre fenetre;
    private GridBagLayout gbl;
    private GridBagConstraints gbc;
    private JPanel pnlResultats;
    private JLabel lblSearch;
    private JCheckBox chkClient;
    private JCheckBox chkEmploye;
    private JCheckBox chkFacture;
    private JCheckBox chkDate;
    private JTextField txtInput;
    private Dimension dimInput;
    private Search recherche;
    private boolean[] parametres;

    public EcranRecherche(Fenetre fenetre) {
        this.fenetre = fenetre;
        //fenetre.setSize(500, 500);
        initComposantes();
        buildInterface();

        fenetre.setTitle(fenetre.getTitle() + " --- Recherche");
        fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        fenetre.setVisible(true);
    }

    private void initComposantes() {
        dimInput = new Dimension(fenetre.getWidth() - 40, 30);

        lblSearch = new JLabel("Rechercher : ");
        chkClient = new JCheckBox("Client", false);
        chkEmploye = new JCheckBox("Employé", false);
        chkFacture = new JCheckBox("Facture", false);
        chkDate = new JCheckBox("Date", false);
        chkClient.addActionListener(new GererParamsRecherche());
        chkDate.addActionListener(new GererParamsRecherche());
        chkEmploye.addActionListener(new GererParamsRecherche());
        chkFacture.addActionListener(new GererParamsRecherche());
        txtInput = new JTextField("");
        txtInput.setPreferredSize(dimInput);
        txtInput.addKeyListener(new GererTouches());

        parametres = new boolean[7];
        for (int i = 0; i < parametres.length; i++) {
            parametres[i] = false;
        }
        pnlResultats = new JPanel();
        pnlResultats.setPreferredSize(new Dimension(450, 300));

        requestFirstFocus();
    }

    private void requestFirstFocus() {
        fenetre.addWindowFocusListener(new WindowAdapter() {

            public void windowGainedFocus(WindowEvent e) {
                txtInput.requestFocusInWindow();
            }
        });
        JFrame trick = new JFrame();
        trick.setVisible(true);
        trick.setVisible(false);
        fenetre.requestFocus();
    }

    private void buildInterface() {
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        this.setLayout(gbl);
        gbc.fill = GridBagConstraints.BOTH;

        makeSeparator();                            //header + checkboxes
        makeBorder();
        gbc.weightx = 0.1;
        gbl.setConstraints(lblSearch, gbc);
        this.add(lblSearch);
        makeBorder();
        gbl.setConstraints(chkClient, gbc);
        this.add(chkClient);
        makeBorder();
        gbl.setConstraints(chkFacture, gbc);
        this.add(chkFacture);
        makeBorder();
        gbl.setConstraints(chkEmploye, gbc);
        this.add(chkEmploye);
        makeBorder();
        gbl.setConstraints(chkDate, gbc);
        this.add(chkDate);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.gridwidth = gbc.RELATIVE;
        gbl.setConstraints(txtInput, gbc);
        this.add(txtInput);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.gridwidth = gbc.RELATIVE;
        gbl.setConstraints(pnlResultats, gbc);
        this.add(pnlResultats);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();
    }

    private void makeSeparator() {
        JLabel sep = new JLabel(" ");
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
        gbc.weighty = 0;
    }

    private void makeBorder() {
        gbc.weightx = 0.1;
        JLabel sep = new JLabel(" ");
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
    }

    public void miseAJour() {
        pnlResultats.removeAll();
        pnlResultats.revalidate();
        pnlResultats.repaint();
        for (int i = 0; i < recherche.getArrResultats2().size() && i < 10; i++) {
            gbc.weightx = 0.1;
            gbc.fill = gbc.HORIZONTAL;
            gbc.gridwidth = gbc.RELATIVE;
            gbl.setConstraints(recherche.getArrResultats2().get(i), gbc);
            ((SearchResult) recherche.getArrResultats2().get(i)).addActionListener(new GererEvenementsRecherche());
            pnlResultats.add(recherche.getArrResultats2().get(i));
            gbc.gridwidth = gbc.REMAINDER;
        }
        pnlResultats.revalidate();
        pnlResultats.repaint();
    }

    class GererEvenementsRecherche implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            SearchResult sr = (SearchResult) e.getSource();
            if (sr.getType() == TypeDonnees.CLIENT) {
                fenetre.afficherConsulterClient(sr.getClient());
            } else if (sr.getType() == TypeDonnees.EMPLOYE) {
                fenetre.afficherModifierEmploye(sr);
            } else if (sr.getType() == TypeDonnees.FACTURE) {
                fenetre.afficherConsulterFacture(sr);
            }
        }
    }

    class GererParamsRecherche implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == chkClient) {
                txtInput.requestFocusInWindow();
                if (chkClient.isSelected()) {
                    parametres[0] = true;
                } else {
                    parametres[0] = false;
                }
            } else if (e.getSource() == chkDate) {
                txtInput.requestFocusInWindow();
                if (chkDate.isSelected()) {
                    parametres[5] = true;
                } else {
                    parametres[5] = false;
                }
            } else if (e.getSource() == chkEmploye) {
                txtInput.requestFocusInWindow();
                if (chkEmploye.isSelected()) {
                    parametres[2] = true;
                } else {
                    parametres[2] = false;
                }
            } else if (e.getSource() == chkFacture) {
                txtInput.requestFocusInWindow();
                if (chkFacture.isSelected()) {
                    parametres[1] = true;
                } else {
                    parametres[1] = false;
                }
            }
            recherche = new Search(fenetre, txtInput.getText(), parametres);
            miseAJour();
        }
    }

    class GererTouches implements KeyListener {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
            if (e.getSource() == txtInput && e.getKeyCode() == e.VK_ENTER) {
                try {
                    ((JButton) pnlResultats.getComponent(0)).doClick();
                } catch (Exception e2) {
                }
            }
            if (e.getSource() == txtInput) {
                recherche = new Search(fenetre, txtInput.getText(), parametres);
                miseAJour();
            }
        }
    }
}
